//
//  PersonalDeleteAlertView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/27.
//

import UIKit

class PersonalDeleteAlertView: UIView {

    typealias ConfirmAction = ()->Void
    private var block: ConfirmAction?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.black.withAlphaComponent(0.15)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showAlertView(handler: ConfirmAction?){
        self.block = handler
        let contentView = UIView()
        let imageView = UIImageView(image: imageWithName("icon_xgl_alert_background"))
        let titleText = UILabel().then { make in
            make.text = "Delete Account"
            make.font = UIFont.pingfangSC_Regular(fontSize: 22.0)
            make.textColor = .white
        }
        let userHeadView = UIImageView().then { make in
            make.layerCornerRadius = 30.0
            make.clipsToBounds = true
            make.layerBorderColor = UIColor.hexColor("#80FFE6")
            make.layerBorderWidth = 1.0
            make.loadImageWithUrl(XGL_userDataManager.share.userHeader)
        }
        let nameText = UILabel().then { make in
            make.text = XGL_userDataManager.share.userName
            make.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.textColor = .white
        }
        let contentText = UILabel().then { make in
            make.textColor = UIColor.hexColor("#A2A2A2")
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.numberOfLines = 0
            make.lineBreakMode = .byWordWrapping
            make.textAlignment = .center
            make.text = "Please confirm whether you need to delete your account. Once identified, all your data will be gone!"
        }
        let dismissButton = UIButton().then { make in
            make.setImage(imageWithName("icon_xgl_alert_close_gray"), for: .normal)
            make.addTarget(self, action: #selector(self.sureAction), for: .touchUpInside)
        }
        let sureButton = UIButton().then { make in
            make.setTitle("Confirm", for: .normal)
            make.setTitleColor(UIColor.hexColor("#191A1D"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.layerCornerRadius = 20.0
            make.addTarget(self, action: #selector(self.sureAction), for: .touchUpInside)
        }
        contentView.addSubviews([imageView,titleText,userHeadView,nameText,contentText,dismissButton,sureButton])
        self.addSubview(contentView)
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        titleText.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(30.0)
        }
        userHeadView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleText.snp.bottom).offset(20.0)
            make.width.height.equalTo(60.0)
        }
        nameText.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(userHeadView.snp.bottom).offset(14.0)
        }
        dismissButton.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.top.equalTo(12.0)
            make.width.height.equalTo(24.0)
        }
        contentText.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(15.0)
            make.top.equalTo(nameText.snp.bottom).offset(12.0)
        }
        sureButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(contentText.snp.bottom).offset(16.0)
            make.height.equalTo(40.0)
            make.width.equalTo(conversionWithScrennWidth(value: 215.0))
        }
        contentView.snp.makeConstraints { make in
            make.width.equalTo(conversionWithScrennWidth(value: 315.0))
            make.bottom.equalTo(sureButton.snp.bottom).offset(22.0)
            make.center.equalToSuperview()
        }
    }
}

extension PersonalDeleteAlertView{
    @objc private func sureAction(){
        self.removeFromSuperview()
    }
}
